package pl.wroc.pwr.nn;
import java.util.Random;

public class Node {

	double[] input;
	double theta = 0;
	double weight[];
	double net;
	double output = 0;
	double eta = 0.7;
	double error;

	Random generator = new Random();

	public Node(int numberOfInputs, float theta) {

		input = new double[numberOfInputs];
		weight = new double[numberOfInputs];

		// losujemy wagi
		for (int i = 0; i < numberOfInputs; i++) {
			weight[i] = generator.nextDouble() * 0.5;
			 //System.out.println("waga: "+weight[i]);

		}
		this.theta = theta;

	}

	public void calcNet() {
		net = 0;
		// System.out.println(weight.length);
		// System.out.println(input.length);
		for (int i = 0; i < input.length; i++) {
			net = net + (weight[i] * input[i]) + theta;
		}

	}

	// funkcja aktywacji 1/1+exp(-net)

	public double output() {
		net = net * (-1);

		// System.out.println(1/(1+Math.exp(-1)));
		return output = (1 / (1 + Math.exp(net)));
	}

	public double calcuErrorOutputLayer(double yZ) {
		//System.out.printf("error: %1.3f \n",(output * (1 - output) * (yZ - output)));
		return error = (output * (1 - output) * (yZ - output));
	}

	public double calcuErrorHiddenLayer(double OutputLayerweight[],
			double OutputLayererror[]) {

		double temp = 0;

		for (int i = 0; i < OutputLayerweight.length; i++) {
			temp = temp + OutputLayerweight[i] * OutputLayererror[i];
			// System.out.println(temp);
		}

		return error = (output * (1 - output) * temp);
	}

	public void setNewWeight() {
		//dSystem.out.print("nowe wagi ");
		for (int i = 0; i < input.length; i++) {
		//	 System.out.printf("%1.2f", error);
			weight[i] = weight[i] + eta * error * input[i];
		//	System.out.printf(" waga: %1.2f \t",weight[i]);

		}
		 //System.out.println("\n----------");
	}

}
